package com.rsstuan.utils;

import java.io.StringReader;
import java.text.DecimalFormat;



public class StringUtils {
   
    private static StringUtils utils = new StringUtils();
    
    public static StringUtils getInstance() {
        return utils;
    }
    
	public static boolean isBlank(String str) {
		if (str == null || str.equals(""))
			return true;
		return false;
	}
	
	public static String cut(String str, int len) {
		if (str == null)
			return "";
	    String ret = removeExtraSpaces(str);
	    if (ret.length() <= len) {
	        return ret;
	    }
	    
	    ret = ret.substring(0, len);
	    ret += "...";
	    return ret;
	}
	
	public static String removeExtraSpaces(String str) {
		String tmp = str.trim();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < tmp.length();) {
			if (tmp.charAt(i) == ' ') {
				sb.append(" ");
				while (tmp.charAt(i) == ' ' && i < tmp.length()) {
					i ++;
				}
			} else {
				sb.append(tmp.charAt(i));
				i ++;
			}
		}
		return sb.toString();
	}
	
	public static String formatDouble(Double dval) {
		DecimalFormat df = new DecimalFormat("0.0");
		return df.format(dval.doubleValue());
	}
	
	public static void main(String args[]) {
	    String s= "                    今日团购：                                        仅售69元  原价1798元的格蕾丝美容超凡盈润水水白+芳香能量肩颈疗程+3S雕体瘦身减肥 ";
	    String t = StringUtils.cut(s, 40);
	    System.out.println(s.trim());
	    System.out.println(t);
	}
}
